package com.hifar.test.synctable8357.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.fw.utils.id.IdUtils;
import com.hifar.plat.officUtil.ExcelTemplateUtil;
import com.hifar.proj.synctable.pojo.SysInfoSyncTable;
import com.hifar.test.synctable8357.mapper.SysInfoSyncTable8357Mapper;
import com.hifar.test.synctable8357.pojo.SysInfoSyncTable8357;
import com.hifar.test.synctable8357.service.ISysInfoSyncTable8357Service;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.RequestConver;

/**
 * <p>
 * 系统-模版表 服务实现类
 * </p>
 *
 * @author wangkepeng
 * @since 2024-11-12 19:27:27
 */
@Service
public class SysInfoSyncTable8357ServiceImpl extends ServiceImpl<SysInfoSyncTable8357Mapper, SysInfoSyncTable8357> implements ISysInfoSyncTable8357Service {


	@Override
	public BusiResponse exportExcel(Map<String, Object> pageInfo) {
		BusiResponse busiResponse = new BusiResponse();
		String ids = (String) pageInfo.get("ids");

		String operationType = (String) pageInfo.get("operationType");

		if (StringUtils.isNotBlank(operationType) && (operationType.equals("template"))) {
			ExcelTemplateUtil.exportExcelTemplate(busiResponse, SysInfoSyncTable.class, "导入模板", "系统-模版表导入数据");
			return busiResponse;
		} else {
			QueryWrapper<SysInfoSyncTable8357> postParam = RequestConver.getPostParam(pageInfo);
			postParam.ne("del_flag", 1);
			postParam.orderByDesc("create_time");
			if (StringUtils.isNotBlank(ids)) {
				postParam.in("id", ids);
			}
			List<SysInfoSyncTable8357> dataList = this.list(postParam);
			if (dataList != null && dataList.size() > 0) {
				ExcelTemplateUtil.exportExcel(busiResponse, dataList, SysInfoSyncTable.class, "系统-模版表数据", "系统-模版表导出");
			} else {
				ExcelTemplateUtil.exportExcelTemplate(busiResponse, SysInfoSyncTable.class, "系统-模版表数据", "系统-模版表导出");
			}
			return busiResponse;
		}

	}

	@Override
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public BusiResponse importExcel(MultipartFile file) throws Exception {
		if (file != null && !file.isEmpty()) {
			List<SysInfoSyncTable8357> importExcel = ExcelTemplateUtil.importExcel(file.getInputStream(), SysInfoSyncTable8357.class);
			List<SysInfoSyncTable8357> importExcelList = new ArrayList<SysInfoSyncTable8357>();
			if (importExcel != null && importExcel.size() > 0) {
				for (SysInfoSyncTable8357 bean : importExcel) {
					bean.setId(IdUtils.uuid32());
					importExcelList.add(bean);
				}
				this.saveBatch(importExcelList);
				return BusiResponse.ok("导入成功！");
			}
		}
		return BusiResponse.error("导入失败，原因未知！");
	}

	@Override
	public List<SysInfoSyncTable8357> listSyncTable() {
		QueryWrapper<SysInfoSyncTable8357> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda()
				.eq(SysInfoSyncTable8357::getStatus, "1")
				.orderByAsc(SysInfoSyncTable8357::getRowSort);
		return this.list(queryWrapper);
	}
}
